package mainFragment.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

import common.BaseActivity;
import common.BaseFragmentAdapter;
import https.handler.ErrorPageHandler;
import mainFragment.bean.AdminssionBean;
import mainFragment.bean.CollegeIndexBean;
import topic.bean.TopicGroupBean;
import mainFragment.bean.ZhaoShengMessageBean;
import mainFragment.fragment.CollegeIntroduce;
import mainFragment.fragment.OnLineAsk;
import mainFragment.fragment.TopicGroup;
import mainFragment.fragment.ZhaoShengMessage;
import mainFragment.fragment.ZhaoShengSituation;
import view.MyPagerSlidingTabStrip;

/**
 * Created by Administrator on 2017/3/3.
 * 高校主页上拉后的页面
 */
@ContentView(R.layout.college_more_detail)
public class CollegeMoreDetail extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.college_more_detail_strip)
    private MyPagerSlidingTabStrip myPagerSlidingTabStrip;
    @ViewInject(R.id.college_more_detail_pager)
    private ViewPager college_more_detail_pager;
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.common_search)
    private ImageView common_search;
    private android.support.v4.app.FragmentManager fragmentManager;
    private BaseFragmentAdapter baseFragmentAdapter;
    private String[] tabs = new String[]{"在线提问", "学校简介", "招生信息", "招生情况", "讨论小组"};
    private OnLineAsk fg1;
    private CollegeIntroduce fg2;
    private ZhaoShengMessage fg3;
    private ZhaoShengSituation fg4;
    private TopicGroup fg5;
    private int tabs_color, tabs_text_size, tabs_color_chosed;
    private ArrayList<Fragment> fragments;
    private int currentItem;//当前选中的位置
    private String collegeId;//当前学校id
    private String name;//当前学校名字
    private final int RESULT_CODE = 1;
    private CollegeIndexBean collegeIndexBean;//高校简介
    private ZhaoShengMessageBean zhaoShengMessageBean;//招生信息
    private AdminssionBean adminssionBean;//招生情况
    private TopicGroupBean topicGroupBean;//热门话题

    private static final String TAG = "CollegeMoreDetail";

    public static final int ONLINEASK_COMPLETE = 1;
    public static final int INTRODUCE_COMPLETE = 2;
    public static final int ZHAOSHENGMESSAGE_COMPLETE = 3;
    public static final int ZHAOSHENGSITUATION_COMPLETE = 4;
    public static final int TOPICGROUPCOMPLETE = 5;

    private ErrorPageHandler myHandler = new ErrorPageHandler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case ONLINEASK_COMPLETE:
                    fg1.setOnLineAskBean(collegeIndexBean);
                    break;
                case INTRODUCE_COMPLETE:
                    fg2.setCollegeIndexBean(collegeIndexBean);
                    break;
                case ZHAOSHENGMESSAGE_COMPLETE:
                    fg3.setZhaoShengMessageBean(zhaoShengMessageBean);
                    break;
                case ZHAOSHENGSITUATION_COMPLETE:
                    fg4.setZhaoShengSituationBean(adminssionBean);
                    fg4.setZhaoShengSituationCoverBean(collegeIndexBean);
                    break;
                case TOPICGROUPCOMPLETE:
                    fg5.setTopicGroup(topicGroupBean);
                    break;
                default:
                    break;
            }
            baseFragmentAdapter.notifyDataSetChanged();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundler = getIntent().getBundleExtra("bundle");
        currentItem = bundler.getInt("currentItem", 0);
        name = bundler.getString("name");
        collegeId = bundler.getString("id");
        collegeIndexBean = (CollegeIndexBean) bundler.getSerializable("OnLineAsk");
        zhaoShengMessageBean = (ZhaoShengMessageBean) bundler.getSerializable("ZhaoShengMessage");
        adminssionBean = (AdminssionBean) bundler.getSerializable("ZhaoShengSituation");
        topicGroupBean = (TopicGroupBean) bundler.getSerializable("TopicGroup");
        initData();
        initView();
        initTabStrip();
    }

    private void initView() {
        common_title.setText(name);
        common_back.setOnClickListener(this);
        common_search.setVisibility(View.INVISIBLE);
    }

    private void initData() {
        fragments = new ArrayList<>();
        fg1 = new OnLineAsk(CollegeMoreDetail.this, collegeId, myHandler);
        fg2 = new CollegeIntroduce(this, myHandler);
        fg3 = new ZhaoShengMessage(CollegeMoreDetail.this, collegeId, myHandler);
        fg4 = new ZhaoShengSituation(CollegeMoreDetail.this, collegeId, myHandler);
        fg5 = new TopicGroup(this, collegeId, myHandler);

        tabs_color_chosed = this.getResources().getColor(R.color.common_color);
        tabs_color = this.getResources().getColor(R.color.tab_color);
        tabs_text_size = this.getResources().getDimensionPixelSize(R.dimen.text_size);
    }

    private void initTabStrip() {
        fragmentManager = this.getSupportFragmentManager();
        fragments.add(fg1);
        fragments.add(fg2);
        fragments.add(fg3);
        fragments.add(fg4);
        fragments.add(fg5);
        baseFragmentAdapter = new BaseFragmentAdapter(fragmentManager, fragments, tabs);
        college_more_detail_pager.setAdapter(baseFragmentAdapter);
        college_more_detail_pager.setOffscreenPageLimit(5);
        myPagerSlidingTabStrip.setTextColor(tabs_color);
        myPagerSlidingTabStrip.setTextSize(tabs_text_size);
        myPagerSlidingTabStrip.setDividerColor(Color.TRANSPARENT);
        myPagerSlidingTabStrip.setDividerPadding(1);
        myPagerSlidingTabStrip.setUnderlineHeight(0);
        myPagerSlidingTabStrip.setIndicatorColor(tabs_color_chosed);
        myPagerSlidingTabStrip.setIndicatorHeight(4);
        myPagerSlidingTabStrip.setTabPaddingLeftRight(32);
        myPagerSlidingTabStrip.setIndicatorPadding(30, 30);
        myPagerSlidingTabStrip.setSelectedTextColor(tabs_color_chosed);
        myPagerSlidingTabStrip.setViewPager(college_more_detail_pager, myHandler);
        college_more_detail_pager.setCurrentItem(currentItem);
        myPagerSlidingTabStrip.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                currentItem = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent data = new Intent();
            data.putExtra("currentItem", currentItem);
            setResult(RESULT_CODE, data);
            CollegeMoreDetail.this.finish();
            CollegeMoreDetail.this.overridePendingTransition(0, 0);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                Intent data = new Intent();
                data.putExtra("currentItem", currentItem);
                setResult(RESULT_CODE, data);
                CollegeMoreDetail.this.finish();
                CollegeMoreDetail.this.overridePendingTransition(0, 0);
                break;
            default:
                break;
        }
    }
}
